<template>
    <div></div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { ElMessage } from 'element-plus';
import { setMiniAppAuth, setFastReg } from '@/api/channelconfig/marketingApplet';

const router = useRouter();
const route = useRoute();

const type = route.query.type;
const auth_code = route.query.auth_code;
const ticket = route.query.ticket;
const official_id = route.query.official_id;
const offical_account_appid = route.query.offical_account_appid;
const id = route.query.id;
let mini_program_type = route.query.mini_program_type || '';
const state: any = route.query.state || '';
if (state) {
    const str = decodeURIComponent(atob(state));
    const info = (str && JSON.parse(str)) || {};
    mini_program_type = info.mini_program_type;
}

onMounted(() => {
    if (type == 'ticket') {
        if (ticket) {
            const params = {
                ticket: ticket,
                official_id: official_id,
                offical_account_appid: offical_account_appid,
                id: id,
                mini_program_type: mini_program_type
            };
            setFastReg(params)
                .then((res: any) => {
                    router.replace({
                        path: '/channelconfig/qywechat/marketingApplet/detail',
                        query: {
                            id: res.id
                        }
                    });
                })
                .catch((err: any) => {
                    ElMessage.error(err);
                });
        } else {
            ElMessage.error('获取ticket失败!');
        }
    } else {
        if (auth_code) {
            const params = {
                auth_code: auth_code,
                mini_program_type: mini_program_type
            };
            setMiniAppAuth(params)
                .then((res: any) => {
                    router.replace({
                        path: '/channelconfig/qywechat/marketingApplet/detail',
                        query: {
                            id: res.id
                        }
                    });
                })
                .catch((err: any) => {
                    ElMessage.error(err);
                });
        } else {
            ElMessage.error('获取code失败!');
        }
    }
});
</script>
